<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <title>image-test</title>
    <meta charset="utf-8" />
    <script src="./js/base64image.js" type="text/javascript"></script>
    <script src="./js/exif.js" type="text/javascript"></script>
</head>

<body>
    <input type="file" style="display: none" onchange="fileUp(this)" id="file-up">
    <input type="button" value="点我上传图片" onclick="document.getElementById('file-up').click();" />
    <div id='image-list'></div>
    <script>
        var fileUp = function(me) {
            console.log(me)
            base64Image({
                width: 750,
                /*【选填】宽度默认750，如果图片尺寸大于该宽度，图片将被设置为该宽度*/
                ratio: 0.75,
                /*【选填】压缩率默认0.75 */
                file: me,
                /*【必填】对应的上传元素 */
                callback: function(imageUrl) { /*【必填】处理成功后的回调函数 */
                    /*imageUrl为得到的图片base64数据，这里可以进行上传到服务器或者其他逻辑操作 */
                    var img = new Image();
                    img.src = imageUrl;
                    console.log(imageUrl)
                    document.getElementById("image-list").appendChild(img);
                    // console.log(img)
                    let blob = dataURLtoFile(imageUrl, 'image/jpeg');
                    // _that.submitPic(blob);
                    let param = new FormData(); //创建form对象
                    param.append('file', blob); //通过append向form对象添加数据
                    console.log(param.get('file')); //FormData私有类对象，访问不到，可以通过get判断值是否传进去
                    if (param) {
                        fetch('http://localhost:8080/upload', {
                            method: 'post',
                            body: param
                        }).then(res => {
                            return res.json();
                        }).then(data => {
                            console.log(data)
                            this.picture = data.filename
                        })
                    }
                },

            });

            function dataURLtoFile(dataURI, type) {
                let binary = atob(dataURI.split(',')[1]);
                let array = [];
                for (let i = 0; i < binary.length; i++) {
                    array.push(binary.charCodeAt(i));
                }
                return new Blob([new Uint8Array(array)], {
                    type: type
                });
            };
        };
    </script>
</body>

</html>